package com.sinog.biz.service.business.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.biz.config.param.Converse;
import com.sinog.biz.mapper.havexml.AreaCodeFullMapper;
import com.sinog.biz.mapper.havexml.SocialEmploymentMapper;
import com.sinog.biz.mapper.havexml.SystemCodeMapper;
import com.sinog.biz.service.business.SocialEmploymentService;
import com.sinog.biz.util.IdGen;
import com.sinog.core.domain.Jyxxmk;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.util.PageReturnUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @desc 安帮工作资源管理 - 社会就业推荐信息管理 - 就业岗位信息管理 - SocialEmploymentServiceImpl
 * @author huoakang
 * @date 2020-02-17
 */
@Service
public class SocialEmploymentServiceImpl extends ServiceImpl<SocialEmploymentMapper,Jyxxmk> implements SocialEmploymentService {

    @Resource
    private SocialEmploymentMapper socialEmploymentMapper;
    @Resource
    private SystemCodeMapper systemCodeMapper;
    @Resource
    private AreaCodeFullMapper areaCodeFullMapper;

    /**
     * 安帮工作资源管理 - 社会就业推荐信息管理 - 就业岗位信息管理 - 列表
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param zwmcKey zwmcKey
     * @param gsmcKey gsmcKey
     * @param gzlxKey gzlxKey
     * @param departId departId
     * @return String
     */
    @Override
    public String getSocialEmploymentList(Integer pageIndex,Integer pageSize,
                                          @Converse(caseIf = "-1", converseTo = "0") String zwmcKey,
                                          @Converse(caseIf = "-1", converseTo = "0") String gsmcKey,
                                          @Converse(caseIf = "-1", converseTo = "0") String gzlxKey,String departId) {
        Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
        page = page.setRecords(socialEmploymentMapper.getSocialEmploymentList(page,zwmcKey,gsmcKey,gzlxKey,departId));
        return PageReturnUtil.pageReturn(page);
    }

    /**
     * 安帮工作资源管理 - 社会就业推荐信息管理 - 就业岗位信息管理 - （修改/新增）
     * @param data data
     * @return Result
     */
    @Override
    public Result save(String data) {
        Jyxxmk jyxxmk = JsonUtil.toBean(data,Jyxxmk.class);
        String provinceCode = jyxxmk.getProvince();
        String cityCode = jyxxmk.getCity();
        String countyCode = jyxxmk.getCounty();
        String placevillCode = jyxxmk.getPlacevill();
        Date date = new Date();
        jyxxmk.setCreateTime(date);
        jyxxmk.setBffwCode(Constants.NumberStr.NUM_STR_FU_ONE.equals(jyxxmk.getBffwCode()) ? null : jyxxmk.getBffwCode());
        if(StringUtils.isEmpty(jyxxmk.getId())) {
            jyxxmk.setId(IdGen.strid());
            jyxxmk.setCreateTime(date);
            jyxxmk.setUpdateTime(date);
        }else {
            jyxxmk.setUpdateUser(jyxxmk.getCreateUser());
            jyxxmk.setUpdateTime(date);
        }
        if(Stream.of(provinceCode,cityCode,countyCode,placevillCode).anyMatch(s -> !StringUtils.isEmpty(s))) {
            jyxxmk.setUpdateUser(jyxxmk.getCreateUser());
            jyxxmk.setUpdateTime(date);
            if (StringUtils.isNotBlank(provinceCode)){
                Map<String,String> areaNames = areaCodeFullMapper.getAreaNames(provinceCode,cityCode,countyCode,placevillCode);
                if (areaNames == null){
                    return Result.failure(ReturnCode.SAVE_FAILED);
                }
                jyxxmk.setProvinceName(areaNames.get("shengName"));
                jyxxmk.setCityName(areaNames.get("dishiName"));
                jyxxmk.setCountyName(areaNames.get("quxianName"));
                jyxxmk.setPlacevillName(areaNames.get("jiedaoName"));
                if (StringUtils.isBlank(cityCode)){
                    jyxxmk.setCityName("");
                }
                if (StringUtils.isBlank(countyCode)){
                    jyxxmk.setCountyName("");
                }
                if (StringUtils.isBlank(placevillCode)){
                    jyxxmk.setPlacevillName("");
                }
            } else {
                jyxxmk.setProvinceName("");
                jyxxmk.setCityName("");
                jyxxmk.setCountyName("");
                jyxxmk.setPlacevillName("");
            }
        }
        boolean flag = jyxxmk.insertOrUpdate();
        if(flag) {
            return Result.success("保存成功");
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 安帮工作资源管理 - 社会就业推荐信息管理 - 就业岗位信息管理 - 删除
     * @param id id
     * @return Result
     */
    @Override
    public Result deleteOne(String id) {
        Jyxxmk jyxxmk = new Jyxxmk();
        jyxxmk.setId(id);
        boolean result = jyxxmk.deleteById();
        if(result) {
            return Result.success("删除成功！");
        }
        return Result.failure(ReturnCode.DELETE_FAILED);
    }

    /**
     * 安帮工作资源管理 - 社会就业推荐信息管理 - 就业岗位信息管理 - 导出
     * @param departId departId
     * @return List<Map<String,Object>>
     */
    @Override
    public List<Map<String,Object>> selectSocialemploymentForExport(String departId) {
        return socialEmploymentMapper.selectSocialemploymentForExport(departId);
    }
}